package cn.IT.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.IT.domain.Approve;
import cn.IT.domain.Knowledge;
import cn.IT.domain.Reply;
import cn.IT.domain.Topic;
import cn.IT.domain.User;
import cn.IT.service.IApproveService;
import cn.IT.service.IReplyService;
import cn.IT.util.JsonUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller()
@Scope("prototype")

public class ApproveAction {
    
	private Approve approve;
	private Topic topic;
	private User user;
	private String searchContent;
	private String resultData; // 用于异步传输数据交互
	
	// 在插件包的情况下直接通过名称注入业务层的类
	@Autowired
	private IApproveService approveService;

    JsonConfig jsonConfig = new JsonConfig();  
    
    //获取审核者待审核的消息
	public String getAllUnapprovedByApprover(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		List<Approve> approves = approveService.getAllUnapprovedByApprover(user);
		if(approves != null){
			resultData = "{\"result\": \"success\"" + "," + "\"approves\":" + JSONArray.fromObject(approves,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//获取用户所有提交的审核信息
	public String getAllApproveByUser(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		List<Approve> approves = approveService.getAllApproveByUser(user);
		if(approves != null){
			resultData = "{\"result\": \"success\"" + "," + "\"approves\":" + JSONArray.fromObject(approves,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//获取审核人的审核历史
	public String getAllApprovedByApprover(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		List<Approve> approves = approveService.getAllApprovedByApprover(user);
		if(approves != null){
			resultData = "{\"result\": \"success\"" + "," + "\"approves\":" + JSONArray.fromObject(approves,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	
	//用户提交审核
	public String addApprove(){
		if (approveService.addApprove(approve) == "success"){
			resultData = "{\"result\": \"success\"" +  "}" ;
			System.out.println(resultData);
		}else{
			resultData = "{\"result\": \"error\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
    //判断问题是否已成功入库
	public String IsApprove(){
		//用户提出的问题已入库
		Approve isApproveExist = approveService.getApprovedByTopic(topic);
		System.out.println(isApproveExist);
		if (isApproveExist != null) {
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"该问题已被推荐入库，请选择其他问题！\""+ "}";
			System.out.println(resultData);
		}
		else{
			resultData = "{\"result\": \"success\"" + "}";
			System.out.println(resultData);
		}
		return "success";
	}
    
	//更新审核信息
	public String updateApprove(){
		if (approveService.updateApprove(approve, user) == "success"){
			resultData = "{\"result\": \"success\"" +  "}" ;
			System.out.println(resultData);
		}else{
			resultData = "{\"result\": \"error\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}

	//根据id获取审核信息
	public String getApproveById(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		Approve comApprove = approveService.getApproveById(approve.getApproveId());
		if( comApprove != null){
			resultData = "{\"result\": \"success\"" + "," + "\"approve\":" + JSONObject.fromObject(comApprove,jsonConfig) +  "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}

	public String getSearchContent() {
		return searchContent;
	}

	public void setSearchContent(String searchContent) {
		this.searchContent = searchContent;
	}

	public void setResultData(String resultData) {
		this.resultData = resultData;
	}
	
    @JSON(serialize=false)
	public String getResultData() {
		return resultData;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Approve getApprove() {
		return approve;
	}

	public void setApprove(Approve approve) {
		this.approve = approve;
	}

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}


}
